<?php
/*
 * Glorious Framework
 *
 * WordPress POMO-based Language manager
 *
 * (c) 2007-2010 Konspire Design
 *
 * $Id: language.php,v 17989674e79b 2010/05/07 08:10:08 and $
 * $Author: and $
 * $Date: 2010/05/07 08:10:08 $
 */

require_once (dirname(__FILE__) . '/pomo/mo.php');
require_once (dirname(__FILE__) . '/pomo/po.php');


class Language extends Singleton
{

    protected $l10n_dir = '';
    protected $l10n_locale = 'ru_RU';
    protected $l10n = array();

    static public function &instance() {
        return parent::instance(__CLASS__);
    }

    protected function __construct()
    {
        $this->l10n_dir = realpath(dirname(__FILE__) . '/../language');

        $conf_l10n_dir = '';
        $conf_l10n_locale = '';

        if(!empty($conf_l10n_dir))
            $this->l10n_dir = $conf_l10n_dir;

        if(!empty($conf_l10n_locale))
            $this->l10n_locale = $conf_l10n_locale;

        $this->l10n_dir = rtrim($this->l10n_dir, '/') . '/';
		
        // Log::instance()->write('L10n Class initialized');

        $this->load_default_textdomain();
    }

    public function get_locale() {
        return $this->l10n_locale;
    }

    public function load_default_textdomain()
    {
        $this->load_textdomain('default', $this->l10n_dir);
    }

    public function load_textdomain($domain, $mofile)
    {
        $mofile = $mofile . $domain . '/' . $this->l10n_locale . '.mo';

        // Log::instance()->write('L10n Loading "%s" textdomain', $domain);

        if(!is_readable($mofile))
            return false;

        $mo = new MO();
        if(!$mo->import_from_file($mofile))
            return false;

        if(isset($this->l10n[$domain]))
            $mo->merge_with($this->l10n[$domain]);

        $this->l10n[$domain] = &$mo;

        return true;
    }

    public function &get_translations_for_domain( $domain ) {
        if(!isset( $this->l10n[$domain])) {
            $this->l10n[$domain] = new NOOP_Translations;
        }

        return $this->l10n[$domain];
    }

    public function translate($text, $domain = 'default') {
        $translations = &$this->get_translations_for_domain($domain);

        return $translations->translate($text);
    }

    public function _x( $single, $context, $domain = 'default' ) {
		return translate_with_gettext_context( $single, $context, $domain );
	}

    public function translate_plural($single, $plural, $number, $domain = 'default') {
        $translations = &$this->get_translations_for_domain( $domain );
        $translation = $translations->translate_plural($single, $plural, $number);

        return $translation;
    }

    public function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
        $translations = &get_translations_for_domain($domain);
        
        return $translations->translate($text, $context);
    }

    public function translate_plural_with_context($single, $plural, $number, $context, $domain = 'default') {
        $translations = &$this->get_translations_for_domain( $domain );
        $translation = $translations->translate_plural( $single, $plural, $number, $context );

        return $translation;
    }


}

/* End of file language.php */
/* Location: ./system/libraries/language.php */